#include <gxgfunc.h>
#define BUFFERSIZE 1024
#define SALTSIZE 8

/*mysql初始化连接*/
MYSQL* mySqlInit() {
    MYSQL* conn = NULL;
    char* host = "localhost";
    char* user = "root";
    char* passwd = "gaoyaguo971118";
    char* db = "GXGNetDisk";

    //初始化mysql的连接句柄
    conn = mysql_init(NULL);

    //建立连接
    if (mysql_real_connect(conn, host, user, passwd, db, 0, NULL, 0) == NULL) {
        printf("error:%s\n", mysql_error(conn));
        return NULL;
    }
    mysql_query(conn, "set names 'utf8'");
    return conn;
}

/*检查是否是用户，如果是用户则传送salt和cryptpasswd*/
bool isUser(MYSQL* conn, const char* userName, char* salt, char* cryptpasswd) {
    int ret;
    char query[BUFFERSIZE] = {0};
    sprintf(query,
            "select u_salt, u_cryptpasswd from UserInfo where u_name = '%s';",
            userName);

    ret = mysql_query(conn, query);
    if (ret != 0) {
        printf("error query1: %s\n", mysql_error(conn));
        return false;
    }

    MYSQL_RES* result = mysql_store_result(conn);
    if (result == NULL) {
        printf("error query2: %s\n", mysql_error(conn));
        return false;
    }

    MYSQL_ROW row = mysql_fetch_row(result);

    //用户不存在
    if (row == NULL) {
        return false;
    }

    //用户存在
    strcpy(salt, row[0]);
    strcpy(cryptpasswd, row[1]);
    puts(cryptpasswd);
    mysql_free_result(result);

    return true;
}

/*获取8位随机值*/
void getRandStr(char* mystr) {
    char str[SALTSIZE + 1] = {0};
    int i, flag;
    srand(time(NULL));
    for (i = 0; i < SALTSIZE; i++) {
        flag = rand() % 3;
        switch (flag) {
            case 0:
                str[i] = rand() % 26 + 'a';
                break;
            case 1:
                str[i] = rand() % 26 + 'A';
                break;
            case 2:
                str[i] = rand() % 10 + '0';
                break;
        }
    }
    strcpy(mystr, str);
}

/*添加用户，用于注册用户*/
int addUser(MYSQL* conn, const char* userName, const char* userPasswd) {
    char salt[20] = {0};
    char cryptpasswd[20] = {0};

    int ret;

    getRandStr(salt);
    strcpy(cryptpasswd, crypt(userPasswd, salt));

    char query[BUFFERSIZE] = {0};
    sprintf(query,
            "insert into UserInfo(u_name, u_salt, u_cryptpasswd, u_pwd) "
            "values('%s', '%s', '%s', '/%s/');",
            userName, salt, cryptpasswd, userName);

    ret = mysql_query(conn, query);
    if (ret != 0) {
        printf("error query1: %s\n", mysql_error(conn));
        return 0;
    }
    return 1;
}

int main() {
    char userName[BUFFERSIZE] = {0};
    char userPasswd[20] = {0};
    char cryptpasswd[20] = {0};
    char salt[20] = {0};

    //连接MYSQL
    MYSQL* conn = mySqlInit();
    ERROR_CHECK(conn, NULL, "mySqlInit");

    //输入用户名和密码
    // gxg 971118
    // hjt 990624
    scanf("%s", userName);
    scanf("%s", userPasswd);

    int isUsrExit = 0;

    //查询用户是否存在
    if (isUser(conn, userName, salt, cryptpasswd)) {
        printf("用户存在!\n");
        isUsrExit = 1;
    } else {
        printf("用户不存在!\n");
    }

    //检验密码
    if (isUsrExit) {
        if (!strcmp(cryptpasswd, crypt(userPasswd, salt))) {
            printf("登陆成功!\n");
        } else {
            puts(crypt(userPasswd, salt));
            printf("密码错误!\n");
        }
    }

    //如果不存在就创建用户
    if (!isUsrExit) {
        int ret = addUser(conn, userName, userPasswd);
        if (ret) {
            printf("创建成功!\n");
        } else {
            printf("创建失败!\n");
        }
    }

    mysql_close(conn);
    return 0;
}